package com.software.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.software.lease.common.exception.LeaseException;
import com.software.lease.common.result.Result;
import com.software.lease.common.result.ResultCodeEnum;
import com.software.lease.model.entity.*;
import com.software.lease.model.enums.ItemType;
import com.software.lease.web.admin.mapper.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.lease.web.admin.service.*;
import com.software.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.software.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.software.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.software.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.software.lease.web.admin.vo.fee.FeeValueVo;
import com.software.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 保存或更新公寓信息
     * @param apartmentSubmitVo
     */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        apartmentSubmitVo.setProvinceName(provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoMapper.selectById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId()).getName());

        super.saveOrUpdate(apartmentSubmitVo);

        Long apartmentId = apartmentSubmitVo.getId();
        if(isUpdate){
            //1. 删除图片列表
            LambdaUpdateWrapper<GraphInfo> graphInfoWrapper = new LambdaUpdateWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
            graphInfoWrapper.eq(GraphInfo::getItemId,apartmentId);
            graphInfoService.remove(graphInfoWrapper);

            //2. 删除配套列表
            LambdaUpdateWrapper<ApartmentFacility> facilityWrapper = new LambdaUpdateWrapper<>();
            facilityWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
            apartmentFacilityService.remove(facilityWrapper);


            //3. 删除杂费列表
            LambdaUpdateWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaUpdateWrapper<>();
            feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
            apartmentFeeValueService.remove(feeValueWrapper);


            //4. 删除标签列表
            LambdaUpdateWrapper<ApartmentLabel> labelWrapper = new LambdaUpdateWrapper<>();
            labelWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
            apartmentLabelService.remove(labelWrapper);


        }
        //1.插入图片列表
        List<com.software.lease.web.admin.vo.graph.GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (com.software.lease.web.admin.vo.graph.GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


        //2.插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }


        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);

        }
    }
    /***
     * 根据id查询公寓详细信息
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //查询基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if(apartmentInfo == null){
            return null;
        }
        //查询图片列表
        List<GraphVo> graphInfoList = graphInfoService.selectByItemId(id,ItemType.APARTMENT);

        //查询配套列表
        List<FacilityInfo> facilityInfoList = apartmentFacilityService.selectByItemId(id);

        //查询标签列表
        List<LabelInfo> labelInfoList = apartmentLabelService.selectByItemId(id);

        //查询杂费列表
        List<FeeValueVo> feeValueVos = apartmentFeeValueService.selectByItemId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        return apartmentDetailVo;
    }

    /**
     * 根据id删除公寓信息
     * @param apartmentId
     */
    @Override
    public void removeByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomInfo::getApartmentId,apartmentId);
        List<RoomInfo> list = roomInfoMapper.selectList(wrapper);
        if(list != null || list.size() > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        super.removeById(apartmentId);
        //1. 删除图片列表
        LambdaUpdateWrapper<GraphInfo> graphInfoWrapper = new LambdaUpdateWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoWrapper.eq(GraphInfo::getItemId,apartmentId);
        graphInfoService.remove(graphInfoWrapper);

        //2. 删除配套列表
        LambdaUpdateWrapper<ApartmentFacility> facilityWrapper = new LambdaUpdateWrapper<>();
        facilityWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
        apartmentFacilityService.remove(facilityWrapper);


        //3. 删除杂费列表
        LambdaUpdateWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaUpdateWrapper<>();
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
        apartmentFeeValueService.remove(feeValueWrapper);


        //4. 删除标签列表
        LambdaUpdateWrapper<ApartmentLabel> labelWrapper = new LambdaUpdateWrapper<>();
        labelWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
        apartmentLabelService.remove(labelWrapper);

    }

    @Override
    public ApartmentInfo selectById(Long apartmentId) {
        return apartmentInfoMapper.selectById(apartmentId);
    }

    @Override
    public IPage<ApartmentItemVo> pageApartment(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> list = apartmentInfoMapper.pageApartment(page,queryVo);
        return list;
    }


}




